package com.webserver.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author 马强
 * @Description
 * @create 2022-04-12 9:08
 */
public class HttpServletResponse {
    private Socket socket;
    //状态行相关信息
    private int statusCode=200; //状态码
    private String statusReason="OK";//
    //响应头相关信息

    //响应正文相关信息
    private File contentFile;

    public HttpServletResponse(Socket socket) {
        this.socket = socket;
    }

    /*
    * 将当前响应对象内容按照标准HTTP响应格式发送给客户端
     */
    public void response() throws IOException {
        //3.1发送状态行
        sendStatusLine();
        //3.2发送响应头
        sendHeaders();
        //发送响应正文（index.html页面的数据）
        sendContent();
    }
    /*发送状态行*/
    private  void sendStatusLine() throws IOException {
        println("HTTP/1.1 "+statusCode+" "+statusReason);
    }
    /*发送响应头*/
    private void sendHeaders() throws IOException {
        println("Content-Type:text/html");
        println("Content-Length:  "+contentFile.length());
        println("");
    }
    /*发送响应正文*/
    private void sendContent() throws IOException {
        OutputStream out =socket.getOutputStream();
        FileInputStream fis = new FileInputStream(contentFile);
        int len;
        byte[] data = new byte[1024*10];
        while((len=fis.read(data))!=-1){
            out.write(data,0,len);
        }
    }

    /*将一行字符串按照iso8859-1编码发给客户端，最后会自动添加回车和换行符
     * @param line*/
    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        out.write(data);
        out.write(13); //单独发送了回车符
        out.write(10);//单独发送了换行符
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
    }
}
